package cn.yunhe.validation;

import cn.yunhe.model.Transaction;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

/**
 * Created by zs on 2017/12/7.
 */
public class TransactionValidator implements Validator {
    @Override
    public boolean supports(Class<?> clazz) {
        //判断验证是否为Transaction，如果是，进行验证
        return Transaction.class.equals(clazz);
    }

    @Override
    public void validate(Object target, Errors errors) {
        Transaction trans = (Transaction) target;
        //求交易金额和价格X数量的差额
        double dis = trans.getAmount() - (trans.getPrice() * trans.getQuantity());
        //如果差额大于0.01，认为业务错误
        if (Math.abs(dis) > 0.01) {
            errors.rejectValue("amount", null, "交易金额和购买数量与价格不匹配");
        }
    }
}
